
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

#include "toolTypes.h"
#include "CRay.h"
//class CSimpleScene;
#include "CMatrix.h"
//#include "SDL.h"

class IPrimitive;

//constante for coordinate system
#define RIGHT_HANDED 1
#define LEFT_HANDED 0

class CRayTracer
{
public:
	enum OVERSAMPLING
	{
		NO_OVERSAMPLING = 1,
		LVL1_OVERSAMPLING = 2,
		LVL2_OVERSAMPLING = 4
	};

private:

	//singleton variable
	static CRayTracer* m_instance;

	//camera 
	CPoint3f m_origin;
	CVector3f m_direction;
	CMatrix m_viewMatrix;

	//screen
	int m_pixelWidth;
	int m_pixelHeight;
	float m_width;
	float m_height;
	float m_fov;

	float m_precompWidth;
	float m_precompHeight;

	float m_precompPixelOffsetX;
	float m_precompPixelOffsetY;

	//CSimpleScene* m_scene;

	OVERSAMPLING m_overSamplingLevel;

	//flag for color swapped. If true, colors are GBR, if false : RGB.
	bool _colorSwapped;

	//flag to true when the rendering loop gotta stop
	bool _stopRendering;

	//flag for right handed coordinate system. If true, +x = right, +y = up, +z=toward. False : +z=backward
	bool _handedness;

private:
	//generate the primary ray for a specific pixel
	void generateRay(int x, int y, CRay& ray)const;

	//generate oversampling rays
	void generateOverSamplingRay(int x, int y, CRay* ray)const;

	void computeColor(const CRay& ray, CColor& color, int reflection, float refractionIndex)const;
	void updateParameters();
	CVector3f getRefractionVector(const CVector3f& normal, const CVector3f& incident, const CVector3f& hit, 
		double n1, double n2)const;
	CVector3f getReflectionVector(const CVector3f& normal, const CVector3f& incident)const;

	float reflectance(const CVector3f& normal, const CVector3f& incident, double n1, double n2)const;

	CRayTracer();
	CRayTracer(int pixelWidth, int pixelHeight, float width, float height, float fov);
	~CRayTracer();

	//Pipeline rendering function returning the intersected primitive.
	inline void PIPELINEgetPrimitive(const CRay& primaryRay, float& dist, CPoint3f& center, IPrimitive** primitive);

	//Pipeline rendering function returning the raw pixel color.
	inline void PIPELINEgetRawColor(const IPrimitive& primitive, const CVector3f& center, CColor& output);

	//Pipeline rendering function calculating the normal
	inline void PIPELINEgetNormal(const IPrimitive& primitive, const CRay& primaryRay, const CPoint3f& center, const float& dist, CVector3f& output);

	//Pipeline rendering function calculating the color of a pixel using light
	inline void PIPELINEcomputeLight(const CVector3f& direction, const CPoint3f& center, const CVector3f& normal, const IPrimitive& primitive,
		const CColor& intput, CColor& output);

	//Pipeline rendering function calculating the transparancy color
	inline void PIPELINEcomputeTransparency(const CRay& incidentRay, float dist,
	const CNormal3f& normal, float incidentRefraction, float reflectedRefraction,
	const CColor& inputColor, CColor& outputColor);

public:
	//singleton gettors and closer
	static CRayTracer& getInstance();
	void close();

	//prepare the raytracer for rendering
	void preDraw();

	//draw the scene
	void draw(char* screenBuffer, int reflection)const;

	//settors
	void setViewer(const CVector3f& eye, const CVector3f& at, const CVector3f& up);
	void setResolution(int pixelWidth, int pixelHeight);
	void setScreen(float ratioWidth, float ratioHeight, float fov);

	void setOverSampling(OVERSAMPLING lvl_ovs);

	//gettors
	int getPixelWidth()const;
	int getPixelHeight()const;
	CMatrix getViewMatrix()const;

	//set the color swapped flag. If true, color order is BGR. If false : RGB
	void setColorSwapped(bool colorSwapped);

	//set the stop signal for the rendering loop
	void setStopSignal(bool stop);

	//set the coordinate system (use LEFT_HANDED or RIGHT_HANDED). Has to be set before the viewer.
	void setCoordinateSystem(bool handedness);

};

#define RAYTRACER CRayTracer::getInstance()
